YAHOO.dasRegistry.startRegistryRequest = function(){
	YAHOO.dasRegistry.setContainers("karyoDas",{KDspecies:"Species from Ensembl",KDsources:"Sources"});
	
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDsources", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDsources");
		
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDspecies", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDspecies");
	
	YAHOO.dasRegistry.listOfSources = new Array();
	
	YAHOO.dasRegistry.sourcesControl = {numberOfRequests:1,successCount:0};
	YAHOO.dasRegistry.makeEnsemblDsnRequest();
}

YAHOO.dasRegistry.startCipfDsnRequest = function(){
	YAHOO.dasRegistry.setContainers("karyoDas",{KDsources:"CIPF Sources"});
	
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDsources", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDsources");

	YAHOO.dasRegistry.listOfSources = new Array();
	
	YAHOO.dasRegistry.sourcesControl = {numberOfRequests:1,successCount:0};
	YAHOO.dasRegistry.makeCipfDsnRequest();
}

YAHOO.dasRegistry.startSangerDsnRequest = function(){
	YAHOO.dasRegistry.setContainers("karyoDas",{KDsources:"Sanger Sources"});
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDsources", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDsources");
	YAHOO.dasRegistry.listOfSources = new Array();
	YAHOO.dasRegistry.sourcesControl = {numberOfRequests:1,successCount:0};
	YAHOO.dasRegistry.makeSangerDsnRequest();
}


YAHOO.dasRegistry.startCipfMixRequest = function(){
	YAHOO.dasRegistry.setContainers("karyoDas",{KDspecies:"Species from Ensembl",KDsources:"Sources"});
	
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDsources", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDsources");
		
	YAHOO.dasBrowser.genome.panel.setPanelListeners("KDspecies", true);
	YAHOO.dasBrowser.genome.panel.setPanelTitleStyle("KDspecies");

	YAHOO.dasRegistry.listOfSources = new Array();
	
	YAHOO.dasRegistry.sourcesControl = {numberOfRequests:2,successCount:0};
	YAHOO.dasRegistry.makeEnsemblDsnRequest();
	YAHOO.dasRegistry.makeCipfDsnRequest();
}



YAHOO.dasRegistry.displayDataTable = function(){
	//console.log("r: "+YAHOO.dasRegistry.sourcesControl.numberOfRequests);
	//console.log("r: "+YAHOO.dasRegistry.sourcesControl.successCount);
	if (YAHOO.dasRegistry.sourcesControl.numberOfRequests == YAHOO.dasRegistry.sourcesControl.successCount) {
		YAHOO.dasRegistry.setSourcesDataTable(YAHOO.dasRegistry.listOfSources, "cipf");
		//YAHOO.dasRegistry.setButton("karyoDasButton", "View annotations in KaryoDAS"); //myKaryoview
		YAHOO.util.Event.addListener("viewButton", "click", viewButtonCallback);
	}
}

YAHOO.dasRegistry.makeCipfDsnRequest = function(){
	YAHOO.dasRegistry.dsnUrl = "http://beta.gepas.bioinfo.cipf.es/dasviewer-1.0.0/das/dsn";
	var url = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?s=" + YAHOO.dasRegistry.dsnUrl;
	YAHOO.dasBrowser.genome.karyotypeStructure.dsn.url = url;
    var request = YAHOO.util.Connect.asyncRequest('GET', url, cipfDsnCallback);
}

YAHOO.dasRegistry.makeSangerDsnRequest = function(){
	YAHOO.dasRegistry.dsnUrl = "http://das.ensembl.org/das/dsn";
	var url = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?s=" + YAHOO.dasRegistry.dsnUrl;
	YAHOO.dasBrowser.genome.karyotypeStructure.dsn.url = url;
    var request = YAHOO.util.Connect.asyncRequest('GET', url, cipfDsnCallback);
}

YAHOO.dasRegistry.makeEnsemblDsnRequest = function(){
	var url = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?s=" + YAHOO.dasBrowser.genome.karyotypeStructure.referenceUrl + "dsn";
	YAHOO.dasBrowser.genome.karyotypeStructure.dsn.url = url;
    var request = YAHOO.util.Connect.asyncRequest('GET', url, ensemblDsnCallback);
}



/**
YAHOO.dasRegistry.makeSourceRequest = function(){
	YAHOO.dasRegistry.userSourceUrl = '';
	var userSourceUrl = new YAHOO.util.Element('usersource');
	userSourceUrl = userSourceUrl.get('value');
	if(userSourceUrl.length > 0){
		YAHOO.dasRegistry.userSourceUrl = userSourceUrl;
		console.log(YAHOO.dasRegistry.userSourceUrl);
		var url = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?s=" + YAHOO.dasRegistry.userSourceUrl;
		//YAHOO.dasBrowser.genome.karyotypeStructure.dsn.url = url;
		alert(url);
		var request = YAHOO.util.Connect.asyncRequest('GET', url, sourceCallback);
	}
}
**/


/*
 * Ensembl DSN Callback
 */
 /**
var sourceCallback = {
	timeout: 10000,
    success:function(o){
		alert("helllooo");
        var sourceResults = YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseSource(o.responseXML);
		//YAHOO.dasBrowser.genome.karyotypeStructure.dsn.allDsn = ensemblDsnResults.allEnsemblDsn;
        //YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.referenceLocation = ensemblDsnResults.referenceLocation;
		//YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation = ensemblDsnResults.karyotypeLocation;
		//console.log(ensemblDsnResults.allEnsemblDsn);
		alert(sourceResults.source.length);
		for (var j = 0; j < sourceResults.source.length; j++) {
				YAHOO.dasRegistry.userSource = new Object();
				YAHOO.dasRegistry.userSource.uri = sourceResults.source[j].uri;
				YAHOO.dasRegistry.userSource.url = YAHOO.dasRegistry.userSourceUrl + "/features";
				YAHOO.dasRegistry.userSource.title =sourceResults.source[j].title;
				YAHOO.dasRegistry.userSource.description =sourceResults.source[j].description;
				YAHOO.dasRegistry.userSource.ntLimit =17000000;
				break;
		}
    },
    failure:function(o){
		YAHOO.example.container.wait.setHeader("User source failure ...");
    	YAHOO.example.container.wait.setBody("Unable to find or understand the user DAS source. Please try again with a different DAS source URL.");
        //console.log("failure: ensemblDsn request");
    }
}
**/


/*
 * Ensembl DSN Callback
 */
var ensemblDsnCallback = {
	timeout: 10000,
    success:function(o){
        var ensemblDsnResults = YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseEnsemblDsn(o.responseXML, YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.id);
		//YAHOO.dasBrowser.genome.karyotypeStructure.dsn.allDsn = ensemblDsnResults.allEnsemblDsn;
        //YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.referenceLocation = ensemblDsnResults.referenceLocation;
		//YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation = ensemblDsnResults.karyotypeLocation;
		//console.log(ensemblDsnResults.allEnsemblDsn);
		
		var referenceSpecies = new Array();
		for (var j = 0; j < ensemblDsnResults.allEnsemblDsn.length; j++) {
			//console.log(ensemblDsnResults.allEnsemblDsn[j].sourceType);
			if(ensemblDsnResults.allEnsemblDsn[j].sourceType == "reference"){
				var dsn = new Object();
				dsn.sourceRelease = ensemblDsnResults.allEnsemblDsn[j].sourceRelease;
				dsn.sourceSpecie = ensemblDsnResults.allEnsemblDsn[j].sourceSpecie.replace(/_/, " ");
				dsn.description = ensemblDsnResults.allEnsemblDsn[j].description;
				dsn.sourceType = ensemblDsnResults.allEnsemblDsn[j].sourceType;
				dsn.sourceUrl = ensemblDsnResults.allEnsemblDsn[j].sourceUrl;
				dsn.sourceId = ensemblDsnResults.allEnsemblDsn[j].sourceId;
				referenceSpecies.push(dsn);
			}
		}
		YAHOO.dasRegistry.setSpeciesDataTable(referenceSpecies);
    },
    failure:function(o){
		YAHOO.example.container.wait.setHeader("DSN request failure ...");
    	YAHOO.example.container.wait.setBody("This software was not able to access the DSN of Ensembl to create the karyotype structure view. We recomend you to try again or come back in few minutes. If the problem persist please contact the <a href=\"http://www.ensembl.org/info/about/contact/index.html\" target=\"_blank\">Ensembl Helpdesk</a>");
        //console.log("failure: ensemblDsn request");
    }
}

//if($label){$url = $url . "&label=" . $label;}
//if($organism){$url = $url . "&organism=" . $organism;}
//if($authority){$url = $url . "&authority=" . $authority;}
//if($capability){$url = $url . "&capability=" . $capability;}
//if($type){$url = $url . "&type=" . $type;}

YAHOO.dasRegistry.makeRegistryRequest = function(){
	var proxyUrl = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl;
	var sourceParameter = YAHOO.dasBrowser.proxy.parameters.source; // s
	var registryUrl = "http://www.dasregistry.org/das1/sources";
	var typeParameter = "Chromosome";
	//var organismParameter = "Homo sapiens";
	var organismParameter = sessvars.karyoDas.conf.specie;
	var url =  proxyUrl + "?s=" + registryUrl + "?type=" + typeParameter + "&organism=" + organismParameter;
	//console.log(url);

    var request = YAHOO.util.Connect.asyncRequest('GET', url, registryRequestCallback);
}


var registryRequestCallback = {
    success:function(o){
		var dasRegistryResults = YAHOO.dasBrowser.dasRegistry.parse(o.responseXML);
		YAHOO.dasRegistry.sourcesControl.successCount++;
		//var dasSources = new Array();
		for (var j = 0; j < dasRegistryResults.sources.length; j++) {
				var source = new Object();
				source.uri = dasRegistryResults.sources[j].uri;
				source.title = dasRegistryResults.sources[j].title;
				source.description = dasRegistryResults.sources[j].description;
				var capability = dasRegistryResults.sources[j].capability;
				for (var i = 0; i < capability.length; i++) {
					if(capability[i].type.indexOf("features") != -1){
						source.url = capability[i].queryUri;
					}
				}
				YAHOO.dasRegistry.listOfSources.push(source);
		}
		YAHOO.dasRegistry.displayDataTable();
    },
    failure:function(o){
		//console.log("failure");
	}
}


var cipfDsnCallback = {
    success:function(o){
		var cipfDsnResults = YAHOO.dasBrowser.genome.karyotypeStructure.dsn.parseDsn(o.responseXML);
		YAHOO.dasRegistry.sourcesControl.successCount++;
		//AQUI ME QUEDE!!!
		//var dsnSources = new Array();
		for (var j = 0; j < cipfDsnResults.allDsn.length; j++) {
			var serverUrl = YAHOO.dasRegistry.dsnUrl.split("/das/dsn");
			var url = serverUrl[0] + "/das/" + cipfDsnResults.allDsn[j].sourceId + "/features";
			
			var source = new Object();
			source.uri = cipfDsnResults.allDsn[j].sourceId;
			source.title = cipfDsnResults.allDsn[j].sourceId;
			source.url = url;
			source.description = cipfDsnResults.allDsn[j].description;
			YAHOO.dasRegistry.listOfSources.push(source);
		}
		YAHOO.dasRegistry.displayDataTable();
    },
    failure:function(o){
		//console.log("failure"); //myKaryoview
	}
}



